package ca.on.eho.qa.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import ca.on.eho.qa.util.Constants;

/*
 * Abstract class representation of a Page in the UI. Page object pattern
 * 
 * Created on: Oct-20-2014
 * Created by: @LS
 * Change log:
 * 
 */

public class PageManager {
	
	private WebDriver driver;

	public LoginPage loginPage;
	public AnyInternalPage anyInternalPage;
	public CSLabResultsPage csLabResultsPage;
	public PSLabResultsPage psLabResultsPage;
	public CSPortlet csPortlet;
	public GooglePage googlePage;
	
	public PageManager(WebDriver driver) {
		this.driver = driver;
		loginPage = initElements(new LoginPage(this));
		anyInternalPage = initElements(new AnyInternalPage(this));
		csLabResultsPage = initElements(new CSLabResultsPage(this));
		psLabResultsPage = initElements(new PSLabResultsPage(this));
		csPortlet = initElements(new CSPortlet(this));
		googlePage = initElements(new GooglePage(this));
	}
	
	private <T extends Page> T initElements(T page) {
		
		// Search elements with implicit wait.
		//PageFactory.initElements(driver,  page);
		
		// Search elements with explicit wait. Return element that Exist.
		//PageFactory.initElements(new AjaxElementLocatorFactory(driver, Constants.SHORT_WAIT),  page);
		
		// Search elements with explicit wait. Return element that Exist and Displayed.
		PageFactory.initElements(new DisplayedElementLocatorFactory(driver, Constants.SHORT_WAIT),  page);
		return page;
	}

	public WebDriver getWebDriver() {
		return driver;
	}

	public String getTitle() {
		return driver.getTitle();
	}

	public PageManager ensurePageLoaded() {
		return this;
	}

	public boolean waitPageLoaded() {
		try {
			ensurePageLoaded();
			return true;
		} catch (TimeoutException te) {
			return false;
		}
	}


}
